package flc.ast.fragment;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import flc.ast.R;
import flc.ast.activity.TimeActivity;
import flc.ast.databinding.FragmentTimeIntervalBinding;
import flc.ast.utils.DateAndTimeDialog;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.utils.TimeUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link TimeIntervalFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class TimeIntervalFragment extends BaseNoModelFragment<FragmentTimeIntervalBinding> implements DateAndTimeDialog.TimePickerDialogInterface {

    private int day = 0, workday = 0;
    private Date startTime, endTime;
    private DateAndTimeDialog dateAndTimeDialog;

    @Override
    protected int onCreate() {
        return R.layout.fragment_time_interval;
    }

    @Override
    protected void initView() {
        Date currentDate = new Date(System.currentTimeMillis());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd EE HH:mm");
        String currentTime = dateFormat.format(currentDate);
        startTime = currentDate;
        endTime = currentDate;
        calculate();
        mDataBinding.tvTimeStart.setText(currentTime);
        mDataBinding.tvTimeEnd.setText(currentTime);
        mDataBinding.llTimeStart.setOnClickListener(this);
        mDataBinding.llTimeEnd.setOnClickListener(this);
        mDataBinding.ivStartCal.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.llTimeStart:
                dateAndTimeDialog = new DateAndTimeDialog(getContext(), R.id.llTimeStart);
                dateAndTimeDialog.showDateAndTimePickerDialog();
                break;
            case R.id.llTimeEnd:
                dateAndTimeDialog = new DateAndTimeDialog(getContext(), R.id.llTimeEnd);
                dateAndTimeDialog.showDateAndTimePickerDialog();
                break;
            case R.id.ivStartCal:
                calculate();
                break;
        }
    }

    public void myPositiveListener(int viewCode) throws ParseException {
        switch (viewCode) {
            case R.id.llTimeStart:
                SimpleDateFormat stringFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
                Date date = stringFormat.parse(dateAndTimeDialog.getDateTime());
                startTime = date;
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd EE HH:mm");
                String dateTime = dateFormat.format(date);
                mDataBinding.tvTimeStart.setText(dateTime);
                break;
            case R.id.llTimeEnd:
                SimpleDateFormat stringFormat1 = new SimpleDateFormat("yyyy/MM/dd HH:mm");
                Date date1 = stringFormat1.parse(dateAndTimeDialog.getDateTime());
                endTime = date1;
                SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy/MM/dd EE HH:mm");
                String dateTime1 = dateFormat1.format(date1);
                mDataBinding.tvTimeEnd.setText(dateTime1);
                break;
        }
    }

    @Override
    protected void initData() {

    }

    private void calculate() {
        //毫秒ms
        long diff = endTime.getTime() - startTime.getTime();

        long diffDays = diff / (24 * 60 * 60 * 1000);

        long diffWeeks = diff / (24 * 60 * 60 * 1000 * 7);
        long diffWeekDays = diff / (24 * 60 * 60 * 1000) % 7;

        long diffMonths = diff / 24 / 60 / 60 / 1000 / 30;
        long diffMonthDays = diff / (24 * 60 * 60 * 1000) % 30;

        long diffYears = diff / 24 / 60 / 60 / 1000 / 365;
        long diffYearMonths = diff / (24 * 60 * 60 * 1000) % 365 / 30;
        long diffYearMonthDays = diff / (24 * 60 * 60 * 1000) % 365 % 30;

        long diffHours = diff / (60 * 60 * 1000);
        long diffHourMinutes = diff / (60 * 1000) % 60;

        long diffMinutes = diff / (60 * 1000);

        mDataBinding.tvTimeIntervalDay.setText(diffDays + getString(R.string.time_interval_text2));
        mDataBinding.tvTimeIntervalWeek.setText(diffWeeks + getString(R.string.time_interval_text4) + diffWeekDays + getString(R.string.time_interval_text2));
        mDataBinding.tvTimeIntervalMonth.setText(diffMonths + getString(R.string.time_interval_text5) + diffMonthDays + getString(R.string.time_interval_text2));
        mDataBinding.tvTimeIntervalYear.setText(diffYears + getString(R.string.time_interval_text6) +
                diffYearMonths + getString(R.string.time_interval_text5)
                + diffYearMonthDays + getString(R.string.time_interval_text2));
        mDataBinding.tvTimeIntervalHour.setText(diffHours + getString(R.string.unit_hour) + diffHourMinutes + getString(R.string.unit_min));
        mDataBinding.tvTimeIntervalMin.setText(diffMinutes + getString(R.string.time_interval_text8));

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startTime);
        int weekdays = 0;
        while (calendar.getTime().getTime() <= endTime.getTime()) {
            int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
            // 如果是周末，则跳过
            if (dayOfWeek == Calendar.SATURDAY || dayOfWeek == Calendar.SUNDAY) {
                calendar.add(Calendar.DATE, 1);
                continue;
            }
            weekdays++;
            calendar.add(Calendar.DATE, 1);
        }
        mDataBinding.tvTimeIntervalWorkDay.setText((weekdays - 1) + getString(R.string.time_interval_text2));
    }

    @Override
    public void positiveListener(int viewCode) {

    }

    @Override
    public void negativeListener() {

    }
}